import React, { Component,useState,useEffect } from 'react';
import { Platform, StyleSheet, Text, View, Image, TouchableOpacity, Dimensions } from 'react-native';
import auth from '@react-native-firebase/auth';
import styles from '../styles/style'
import { TextInput } from 'react-native-gesture-handler';
import Global from '../utils/global';

export default function Login({navigation}){
    const [email,setEmail] = useState('leon2007beyond@gmail.com')
    const [password,setPassword] = useState('lalalala')

    const  navigationOptions = ({ navigation }) => ({
        headerTitle: 'Index1',
        // headerLeft: (<TouchableOpacity style={{ marginLeft: 5 }} onPress={() => navigation.goBack()}><IconFont name='arrow-left' size={20} /></TouchableOpacity>),
    });

    const login = ()=>{
        auth().signInWithEmailAndPassword(email, password)
            .then(() => {
                console.log('User account created & signed in!');
                auth().currentUser.getIdToken(false).then(token => {
                    Global.token = token;
                    console.log(token)
                    navigation.push('List');
                })
            })
            .catch(error => {
                if (error.code === 'auth/email-already-in-use') {
                    console.log('That email address is already in use!');
                }

                if (error.code === 'auth/invalid-email') {
                    console.log('That email address is invalid!');
                }

                if (error.code === 'auth/user-not-found') {
                    signup()
                }

                console.error(error);
            });
    }

    const signup=()=> {
        auth().createUserWithEmailAndPassword(email, password)
            .then(() => {
                console.log('User account created & signed in!');
                auth().currentUser.getIdToken(false).then(token => {
                    Global.token = token;
                    console.log(token)
                    navigation.push('List');
                })
            })
            .catch(error => {
                if (error.code === 'auth/email-already-in-use') {
                    console.log('That email address is already in use!');
                }

                if (error.code === 'auth/invalid-email') {
                    console.log('That email address is invalid!');
                }

                console.error(error);
            });
    }
    
    return (
        <View style={styles.container}>
            <TextInput placeholder='email'
                keyboardType='email-address'
                value={email}
                onChangeText={(text) => { setEmail(text) }}
                style={styles.input}></TextInput>
            <TextInput placeholder='password'
                secureTextEntry={true}
                style={styles.input}
                value={password}
                onChangeText={(text) => { setPassword(text) }}
                style={styles.input}></TextInput>
            <View style={styles.row_center}>
                <TouchableOpacity style={styles.start_btn} onPress={login}>
                    <Text style={[styles.text_white, styles.text_bold]}>Login</Text>
                </TouchableOpacity>
            </View>
        </View>
    );
}